<template>
  <div>
    <el-dialog :title="info.isAdd ? '菜单添加' : '菜单修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select v-model="user.pid" placeholder="请选择上级菜单" @change="changepid">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in arr" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" label-width="120px">
          <el-radio v-model="user.type" disabled :label="1">目录</el-radio>
          <el-radio v-model="user.type" disabled :label="2">菜单</el-radio>
        </el-form-item>
        <!-- 目录出现图标 -->
        <el-form-item label="菜单图标" label-width="120px" v-if="user.type == 1">
          <el-select v-model="user.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 菜单出现地址 -->
        <el-form-item label="菜单地址" label-width="120px" v-if="user.type == 2">
          <el-select v-model="user.url" placeholder="请选择菜单地址">
            <el-option
              v-for="item in IndexRoutes"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { reqmenuadd, reqmenuinfo, reqmenuedit } from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";

export default {
  props: ["info", "arr"],
  mixins: [formClose],
  data() {
    return {
      user: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1
      },
      icons: [
        "el-icon-goods",
        "el-icon-setting",
        "el-icon-s-help",
        "el-icon-upload",
        "el-icon-camera"
      ],
      IndexRoutes
    };
  },
  computed: {
    ...mapGetters({})
  },
  mounted() {},
  methods: {
    ...mapActions({}),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.title == "") {
          errorAlert("菜单名称不能为空");
          return;
        }
        if (this.user.second_cateid == "") {
          errorAlert("上级菜单不能为空");
          return;
        }

        if (this.user.pid === 0) {
          if (this.user.icon == "") {
            errorAlert("菜单图标不能为空");
            return;
          }
        } else {
          if (this.user.url == "") {
            errorAlert("菜单地址不能为空");
            return;
          }
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        reqmenuadd(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    },
    empty() {
      this.user = {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1
      };
    },
    changepid() {
      if (this.user.pid === 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    getOne(id) {
      reqmenuinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        reqmenuedit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
</style>